import time
# from resultJudgment import resultJudgment 
# class Transfer(data):


# groupManager = {}

# selectIndex = 0

def getSelectIndex(index):
    print("选中的group是",index)


# def getDataArray(arr):
#     print("切换group时的arr ===",arr)


def showDatas(treeview,datas,resultArray,index):

    # for item in datas:
    #     print(item)
    for item in datas:
        testDict = item
        upperLimit = 0
        value = 0
        lowerLimit = 0
        if testDict["upperLimit"] != "":
            upperLimit = int(testDict["upperLimit"])
        if testDict["value"]:
            print("---main---value :",testDict["value"])
        if testDict["value"] == False or testDict["value"] == True:
            value = testDict["value"]
        else:
            try:
                value = float(testDict["value"])
            except ValueError as e:
                value = testDict["value"]
            
        if testDict["lowerLimit"] != "":
            lowerLimit = float(testDict["lowerLimit"])
        
        passResult = "fail"
        # 上下限都有
        if testDict["upperLimit"] != "" and testDict["lowerLimit"] != "":
            print("上下限都有")
            if value >= lowerLimit and value <= upperLimit:
                passResult = "pass"

        # 只有上限
        if testDict["upperLimit"] != "" and testDict["lowerLimit"] == "":
            print("只有上限")
            if value < upperLimit:
                passResult = "pass"
        # 只有下限
        if testDict["upperLimit"] == "" and testDict["lowerLimit"] != "":
            print("只有下限")
            if value > lowerLimit:
                passResult = "pass"

        if testDict["upperLimit"] == "" and testDict["lowerLimit"] == "":
            passResult = "pass"
        if value == True:
            passResult = "pass"
        if value == False:
            passResult = "fail"

        tag = testDict["id"]
        gridValues = (
                        f"{tag}",
                        testDict["TestName"],
                        testDict["subTestName"],
                        testDict["subSubTestName"],
                        testDict["upperLimit"],
                        testDict["value"],
                        testDict["lowerLimit"],
                        passResult,
                        "no info",
                        "10"
        )
        
        recordColorTags = ""
        if passResult == "fail":
            print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
            recordColorTags = "red"
            treeview.insert('',tag,values=gridValues,tags=recordColorTags)
            print(index)
            resultArray[int(index)] = "1"
            print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
            print(resultArray)
        else:
            treeview.insert('',tag,values=gridValues,)
        
        treeview.tag_configure("red",background="#ff0000")
        # treeview.update()

def checkRecord(param):
    print("start checkRecord")


    datas = []
    for item in param["dataArray"]:
        for key_name in item.keys():
            print(key_name)
            if str(param["groupIndex"]) == key_name:
                print(param["groupIndex"])
                # print(param["groupIndex"])
                print(item)
                print("----------------------")
                # print(item[int(param["groupIndex"])])
                datas = item[str(param["groupIndex"])]


    for item in datas:
        testDict = item
        upperLimit = 0
        value = 0
        lowerLimit = 0
        if testDict["upperLimit"] != "":
            upperLimit = int(testDict["upperLimit"])
        if testDict["value"]:
            print("---main---value :",testDict["value"])
        if testDict["value"] == False or testDict["value"] == True:
            value = testDict["value"]
        else:
            try:
                value = float(testDict["value"])
            except ValueError as e:
                value = testDict["value"]

        if testDict["lowerLimit"] != "":
            lowerLimit = int(testDict["lowerLimit"])
        
        passResult = "fail"
        # 上下限都有
        if testDict["upperLimit"] != "" and testDict["lowerLimit"] != "":
            print("上下限都有")
            if value in range(lowerLimit,upperLimit):
                passResult = "pass"

        # 只有上限
        if testDict["upperLimit"] != "" and testDict["lowerLimit"] == "":
            print("只有上限")
            if value < upperLimit:
                passResult = "pass"
        # 只有下限
        if testDict["upperLimit"] == "" and testDict["lowerLimit"] != "":
            print("只有下限")
            if value > lowerLimit:
                passResult = "pass"

        if testDict["upperLimit"] == "" and testDict["lowerLimit"] == "":
            passResult = "pass"

        tag = testDict["id"]
        gridValues = (
                        f"{tag}",
                        testDict["TestName"],
                        testDict["subTestName"],
                        testDict["subSubTestName"],
                        testDict["upperLimit"],
                        testDict["value"],
                        testDict["lowerLimit"],
                        passResult,
                        "no info",
                        "10"
        )

        recordColorTags = ""
        if passResult == "fail":
            param["resultArray"][int(param["groupIndex"])] = "1"
            print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+++++++++")
            print(param["resultArray"])
      



def insertRecord(param,testDict):

    upperLimit = 0
    value = 0
    lowerLimit = 0
    if testDict["upperLimit"] != "":
        upperLimit = int(testDict["upperLimit"])
    if testDict["value"] != "":
        value = int(testDict["value"])

    if testDict["lowerLimit"] != "":
        lowerLimit = int(testDict["lowerLimit"])
    
    passResult = "fail"
    # 上下限都有
    if testDict["upperLimit"] != "" and testDict["lowerLimit"] != "":
        print("上下限都有")
        if value in range(lowerLimit,upperLimit):
            passResult = "pass"

     # 只有上限
    if testDict["upperLimit"] != "" and testDict["lowerLimit"] == "":
        print("只有上限")
        if value < upperLimit:
            passResult = "pass"
    # 只有下限
    if testDict["upperLimit"] == "" and testDict["lowerLimit"] != "":
        print("只有下限")
        if value > lowerLimit:
            passResult = "pass"

    if testDict["upperLimit"] == "" and testDict["lowerLimit"] == "":
        passResult = "pass"

    tag = testDict["id"]
    gridValues = (
                    f"{tag}",
                    testDict["TestName"],
                    testDict["subTestName"],
                    testDict["subSubTestName"],
                    testDict["upperLimit"],
                    testDict["value"],
                    testDict["lowerLimit"],
                    passResult,
                    "no info",
                    "10"
    )

    recordColorTags = ""
    if passResult == "fail":
        recordColorTags = "red"
        param["tableTree"].insert('',tag,values=gridValues,tags=recordColorTags)
    else:
        param["tableTree"].insert('',tag,values=gridValues,)
    
    param["tableTree"].tag_configure("red",background="#ff0000")
    param["tableTree"].update()